package pannel.product;

import system.BasePage;
import pannel.user.User;

import javax.swing.*;
import java.awt.*;
import java.sql.SQLException;
import java.util.List;

public class ProductListPage extends BasePage {
    private ProductDao productDao;
    private JPanel cardsPanel;
    private JTextField searchField;
    private int currentPage = 1;
    private int pageSize = 12;  // 每页显示12个商品卡片
    private JLabel pageInfoLabel;
    private int totalRecords = 0;

    public ProductListPage(User user) {
        super(user);
        productDao = new ProductDao();
        initPageContent();
    }

    @Override
    protected void initPageContent() {
        setPageTitle("商品列表");
        setupContent();
        refreshProducts();
    }

    private void setupContent() {
        contentArea.setLayout(new BorderLayout(10, 10));
        contentArea.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        // 搜索面板
        JPanel searchPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        searchField = new JTextField(20);
        JButton searchButton = new JButton("搜索");
        searchPanel.add(new JLabel("商品名称："));
        searchPanel.add(searchField);
        searchPanel.add(searchButton);

        // 商品卡片面板
        cardsPanel = new JPanel(new GridLayout(0, 4, 20, 20)); // 4列，自动行数
        cardsPanel.setBackground(Color.WHITE);
        JScrollPane scrollPane = new JScrollPane(cardsPanel);
        scrollPane.setBorder(null);
        scrollPane.getVerticalScrollBar().setUnitIncrement(16);

        // 分页面板
        JPanel paginationPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        JButton prevButton = new JButton("上一页");
        JButton nextButton = new JButton("下一页");
        pageInfoLabel = new JLabel();
        paginationPanel.add(prevButton);
        paginationPanel.add(pageInfoLabel);
        paginationPanel.add(nextButton);

        // 按钮事件
        searchButton.addActionListener(e -> {
            currentPage = 1;
            refreshProducts();
        });

        prevButton.addActionListener(e -> {
            if (currentPage > 1) {
                currentPage--;
                refreshProducts();
            }
        });

        nextButton.addActionListener(e -> {
            int totalPages = (int) Math.ceil((double) totalRecords / pageSize);
            if (currentPage < totalPages) {
                currentPage++;
                refreshProducts();
            }
        });

        contentArea.add(searchPanel, BorderLayout.NORTH);
        contentArea.add(scrollPane, BorderLayout.CENTER);
        contentArea.add(paginationPanel, BorderLayout.SOUTH);
    }

    private void refreshProducts() {
        SwingUtilities.invokeLater(() -> {
            try {
                String searchText = searchField.getText().trim();
                List<Product> products = productDao.pageQuery(searchText, currentPage, pageSize);
                totalRecords = productDao.getTotalCount(searchText);

                cardsPanel.removeAll();
                for (Product product : products) {
                    ProductCard card = new ProductCard(product);
                    cardsPanel.add(card);
                }

                updatePageInfo();
                cardsPanel.revalidate();
                cardsPanel.repaint();
            } catch (SQLException e) {
                JOptionPane.showMessageDialog(this, "获取商品数据失败：" + e.getMessage());
            }
        });
    }

    private void updatePageInfo() {
        int totalPages = (int) Math.ceil((double) totalRecords / pageSize);
        pageInfoLabel.setText(String.format("第 %d/%d 页  共%d条记录", currentPage, totalPages, totalRecords));
    }

    // 使用父类的getCurrentUser方法
    @Override
    public User getCurrentUser() {
        return super.getCurrentUser();
    }
} 